class Documents::Show::ReshipsController < ApplicationController
  def show
    @reships = Reship.paginate :page => params[:reship_page_number], :per_page => 5
    @documet_types = default_category + DocumetType.all_valleyer_id_is_not_null_documet_types
  end

  def index
    @reships = Reship.paginate :page => params[:reship_page_number], :per_page => 5
  end
  
  def new
    @reship = Reship.new :author_id => current_valleyer_id
  end

  def create
    @reship = Reship.new params[:reship]
    if params[:document_type][:name].blank?
      @reship.documet_type = DocumetType.find_by_name "未分类"
    else
      @documet_type = DocumetType.find_by_name params[:document_type][:name]
      if @documet_type.nil?
        @reship.documet_type = DocumetType.new :name => params[:document_type][:name],
          :valleyer_id => current_valleyer_id
        @reship.documet_type.save
      else
        @reship.documet_type = @documet_type
      end
    end
    @reship.save
    @valleyer = current_valleyer
    @reships = Reship.paginate :page => params[:page_number], :per_page => 5
    @documet_types = default_category + @valleyer.documet_types
  end

  def edit
    @reship = Reship.find params[:id]
    @current_page = params[:current_page]
    @document_type = @reship.documet_type
  end

  def update
    @reship = Reship.find params[:id]
    if params[:document_type][:name].blank?
      @reship.documet_type = DocumetType.find_by_name "未分类"
    else
      @documet_type = DocumetType.find_by_name params[:document_type][:name]
      if @documet_type.nil?
        @reship.documet_type = DocumetType.new :name => params[:document_type][:name],
          :valleyer_id => current_valleyer_id
        @reship.documet_type.save
      else
        @reship.documet_type = @documet_type
      end
    end
    render :edit unless @reship.update_attributes params[:reship]
    @valleyer = current_valleyer
    @reships = Reship.paginate :page => params[:page_number], :per_page => 5
    @documet_types = default_category + @valleyer.documet_types
  end

  def destroy
    @reship = Reship.find params[:id]
    @valleyer = @reship.author
    @reship.destroy
    @reships = Reship.paginate_by_author_id @valleyer.id,
      :page => params[:page_number],
      :per_page => 5,
      :order => "created_at desc"
    @documet_types = default_category + @valleyer.documet_types
  end

  def authorize

  end
end
